package cn.itsource.pethome.system.menu.service.impl;



import cn.itsource.pethome.system.menu.domain.Menu;
import cn.itsource.pethome.system.menu.mapper.MenuMapper;
import cn.itsource.pethome.system.menu.service.IMenuService;
import cn.itsource.pethome.base.service.impl.IBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xlh
 * @since 2023-02-01
 */
@Service
public class MenuServiceImpl extends IBaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private  MenuMapper menuMapper;

    @Override
    public List<Menu> getTree() {
        //第一步查询全部
        List<Menu> all = menuMapper.findAll();
        //儿子找父亲
        for(Menu menu : all){
            if (menu.getParentId() != null){
                for(Menu parent : all){
                    if (menu.getParentId() == parent.getId()){
                        List<Menu> children = parent.getChildren();
                        children.add(menu);
                        break;
                    }
                }
            }
        }
        //找顶级
        List<Menu> firstTree = all.stream()
                .filter(menu -> menu.getParentId() == null)//过滤出来父id为null
                .collect(Collectors.toList());//转为list
        return firstTree;
    }

    @Override
    public List<Long> findMenuByRole(List<Long> roles) {
        return menuMapper.findMenuByRole(roles);
    }
}
